import getIn from '../../../yali-sdk/utils/getIn';

Component({
    properties: {
        searchBarEvent: {
            type: Function,
        },
        items: {
            type: Array,
        
        },
    },
    data: {
        isSearch: false,
        input: '',
        current: -1,
    },
    methods: {
        // 
        onSelect(e) {
            const idx = getIn(e, ['currentTarget', 'dataset', 'idx']);
            const liveing = getIn(e, ['currentTarget', 'dataset', 'liveing']);
            if (idx === this.data.current) {
                this.hideModal();
            } else {
                this.showModal(idx);
            }

        },
        onChildClick(e) {
            const idx = getIn(e, ['currentTarget', 'dataset', 'idx']);
            const cidx = getIn(e, ['currentTarget', 'dataset', 'cidx']);
            const liveing = getIn(e, ['currentTarget', 'dataset', 'liveing']);
            const web_id = getIn(e, ['currentTarget', 'dataset', 'web_id']);
            const items = this.data.items.map((it, i) => {
                if (i === idx) {
                    return { ...it, current: cidx, liveing, web_id };
                }
                return it;
            });
            this.setData({ items });
            this.hideModal();
            this.trigger(items);
        },
        onSearch() {
            this.setData({ isSearch: true });
        },
        onSearchCancel() {
            this.setData({ input: '', isSearch: false });
        },
        bindinput(e) {
            this.setData({ input: e.detail.value });
        },
        bindconfirm() {
            this.triggerEvent('searchBarEvent', {
                type: 'input',
                keywords: this.data.input,
            });
            this.onSearchCancel();
        },
        trigger(items) {
            this.triggerEvent('searchBarEvent', {
                type: 'click',
                items,
            });
        },


        /** 弹出优惠券弹窗**/
        showModal(current) {
            this.timers && clearTimeout(this.timers);
            const animation = wx.createAnimation({
                duration: 200,
                timingFunction: 'linear',
                delay: 0,
            });
            this.animation = animation;
            animation.translateY(-200).step();
            this.setData({ animationData: animation.export(), current });
            this.times = setTimeout(() => {
                animation.translateY(0).step();
                this.setData({
                    animationData: animation.export(),
                });
            }, 200);
        },
        // 取消弹出框
        hideModal() {
            this.timers && clearTimeout(this.timers);
            const animation = wx.createAnimation({
                duration: 200,
                timingFunction: 'linear',
                delay: 0,
            });
            this.animation = animation;
            animation.translateY(-200).step();
            this.setData({ animationData: animation.export() });
            this.times = setTimeout(() => {
                animation.translateY(0).step();
                this.setData({
                    animationData: animation.export(),
                    current: -1,
                });
            }, 200);
        },
    },
});
